<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="750" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm" >
      <!-- <template #accessSecret="{ model, field, schema }">
        <a-row :gutter="8">
          <a-col :sm="16" :md="16" :lg="17" :xl="19">
            <ApiSelect         
              v-model:value="model[field]"
              optionFilterProp="label"
              resultField="list"
              v-bind="schema.componentProps"
            />
          </a-col>
          <a-col :sm="8" :md="7" :lg="7" :xl="5">
            <a-button type="primary" style="width: 100%" >管理</a-button>
          </a-col>
        </a-row>
      </template> -->
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import {ref, computed, unref,useAttrs} from 'vue';
  import {BasicForm, useForm} from '/@/components/Form/index';
  import {formSchema} from './mchApp.data';
  import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  import {saveOrUpdateMch, getMchApp} from './mchApp.api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { ApiSelect } from '/@/components/Form/index';
  const { createMessage } = useMessage();
  // 声明Emits
  const emit = defineEmits(['success', 'register']);
  const attrs = useAttrs()
  const isUpdate = ref(true);
  const rowId = ref('');
  //表单配置
  const [registerForm, {setProps,resetFields, setFieldsValue, validate, updateSchema}] = useForm({
    labelWidth: 100,
    wrapperCol: {
      md: { span: 22 },
      sm: { span: 22 },
    },
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  
  //表单赋值
  const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
    await resetFields();
    let showFooter = data?.showFooter ?? true
    setDrawerProps({ confirmLoading: false, showFooter })
    isUpdate.value = !!data?.isUpdate;
    let mchInfo : any = {}
    if (unref(isUpdate)) {
      rowId.value = data.record.mchNo;
      mchInfo = await getMchApp({id: data.record.id})
    } else {
      mchInfo = {...mchInfo, mchNo: data.mchNo}
    }
    updateSchema([
    {
        field: 'appSecret',
        label: '应用私钥',
        required: !isUpdate.value,
        componentProps: {
          placeholder: mchInfo.appSecret,
        },
        component: 'InputTextArea',
      },
    ])
    mchInfo.appSecret = ""
    // 无论新增还是编辑，都可以设置表单值
    if (typeof mchInfo === 'object') {
      setFieldsValue({
        ...mchInfo,
      })
    }
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !showFooter })
  });
  //获取标题
  const getTitle = computed(() => (!unref(isUpdate) ? '新增商户应用' : '编辑商户应用'));

  //提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setDrawerProps({confirmLoading: true});
      values.userIdentity === 1 && (values.deptIds='');
      //提交表单
      await saveOrUpdateMch(values, unref(isUpdate));
      //关闭弹窗
      closeDrawer();
      createMessage.success(`${getTitle.value}成功`)
      //刷新列表
      emit('success', {isUpdate: unref(isUpdate), values: {...values, id: rowId.value}});
    } finally {
      setDrawerProps({confirmLoading: false});
    }
  }
</script>
